using UnityEngine;
using System.Collections;

public class Opening : MonoBehaviour {

    int id = 0;
    GameObject fade_in;
    GameObject fade_out;

    float count_change_scene;

    void Start()
    {
        this.gameObject.AddComponent("MeshRenderer");
        this.gameObject.AddComponent("MeshFilter");


        Mesh mesh = Quad.Create(4.0f, 3.0f);

        this.gameObject.GetComponent<MeshFilter>().mesh = mesh;
        this.gameObject.GetComponent<MeshRenderer>().material = AtlasManager.globalMaterial;

        this.gameObject.GetComponent<MeshFilter>().mesh.uv = new Vector2[]
		{
			new Vector2(AtlasManager.rects[id].xMin, AtlasManager.rects[id].yMax),
			new Vector2(AtlasManager.rects[id].xMax, AtlasManager.rects[id].yMax),
			new Vector2(AtlasManager.rects[id].xMax, AtlasManager.rects[id].yMin),
			new Vector2(AtlasManager.rects[id].xMin, AtlasManager.rects[id].yMin)
		};

        fade_in = new GameObject("Fade_In", typeof(Fade_In));
        Fade_Out.fade_out_begin = false;
        Fade_Out.fade_out_terminated = false;
    }

    void Update()
    {
        if (Fade_In.fade_in_terminated)
        {
            count_change_scene += Time.deltaTime;
        }

        if (count_change_scene > 3)
        {
            if (!Fade_Out.fade_out_begin)
            {
                fade_out = new GameObject("Fade_Out", typeof(Fade_Out));
            }

            if (Fade_Out.fade_out_terminated)
            {
				Application.LoadLevel("Menu");
            }
        }
    }
}
